﻿//创建时间: 2010-12-15
//创建作者: 李 城
//功能说明: cms外部页面访问基类 生成验证码
//最后修改: 2010-12-15
using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using TRPOP.CMS.Business;
using TRPOP.CMS.Model;
using System.Web;

using TRPOP.Config;

namespace TRPOP.CMS.Web.ExtraBus
{
    /// <summary>
    /// 
    /// </summary>
    public class Page_Load_RecommentCheckCode : BasePage
    {
        /// <summary>
        /// 生成验证码
        /// </summary>
        /// <param name="e"></param>
        protected override void OnInit(EventArgs e)
        {
            string strRecommentCheckCodeUrl = SYSConfig.RecommentCheckCodeUrl;
            string  strDynamicPageName = 
            (base.mCurrentUrl.ToLower().Contains(strRecommentCheckCodeUrl.ToLower().Trim())) ? "RecommentCheckCodeUrl" : "";
            if (strDynamicPageName == "RecommentCheckCodeUrl")
                DrawImageCookie("RecommentCookie", 4);
            else
                Base_Config.NotFound(base.Page, "CMS后台配置的页面地址和访问的页面不一致(验证码)");
            //base.OnInit(e);
        }
        /// <summary>
        /// 生成验证码并保存到Cookie
        /// </summary>
        /// <param name="CookieName">Cookie名</param>
        /// <param name="length">长度</param>/param>
        public void DrawImageCookie(string CookieName, int length)
        {
            Response.Expires = -1;
            Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
            Response.CacheControl = "no-cache";
            #region 写Cookie
            string strRandom = Common.Util.GetRandomCode(length);
            HttpCookie cookie = new HttpCookie(CookieName);
            cookie.Value = strRandom;
            string strCookieDomain = HttpContext.Current.Request.Url.Authority;
            strCookieDomain = strCookieDomain.Substring(strCookieDomain.IndexOf('.') + 1);
            cookie.Domain = strCookieDomain;
            HttpContext.Current.Response.AppendCookie(cookie);
            #endregion
            System.IO.MemoryStream ms = new System.IO.MemoryStream();
            ms = Common.Util.IdentifyImg(strRandom, length, 20);
            Response.Clear();
            Response.ContentType = "image/jpeg";

            Response.BinaryWrite(ms.ToArray());
            Response.End();
        }
    }
}
